var _zcms_recommend_d,_zcms_type,_zcms_pagesize,_zcms_filltype,_zcms_classid,_zcms_template,_zcms_pageindex;
var _zcms_recommend = function(param){
	var p = {};
	if(param){
		var arr = param.split("&");
		for(var i=0;i<arr.length;i++){
			if(arr[i]){
				var arr2 = arr[i].split("=");
				if(arr2[0]){
					p[arr2[0]] = arr2[1];
				}
			}
		}
	}

	_zcms_recommend_d = p["dest"];
	_zcms_type = p["type"];
	_zcms_pagesize = p["pageSize"];
	_zcms_pageindex = 0;
	_zcms_filltype = p["fillType"];
	_zcms_classid = p["classID"];
	_zcms_siteid = p["siteID"];
	_zcms_ispreview = p["isPreview"];
	_zcms_template = $("#"+_zcms_classid).html();
	if(_zcms_template == ''){
		_zcms_template = '<li><a href="@(link)">@(title)</a></li>';
	}
	var memberID=_zcms_recommend.cookieGet("_ZMemberID");
	if(memberID){
		p["MemberID"] = memberID;
	}
	
	$.ajax({
        type: 'GET',
        url: _zcms_recommend_d,
        data: {
		  memberID: memberID,
		  siteID: _zcms_siteid,
		  isPreview: _zcms_ispreview,
          type: _zcms_type,
          pagesize: _zcms_pagesize,
		  pageindex: _zcms_pageindex,
		  fillType: _zcms_filltype
        },
		crossDomain : true==!(document.all),
		xhrFields: {
			withCredentials: true
		},
        dataType: 'json',
        success: function (result) {
			var html = '';
			var ids = [];
			$.each(result.data.data, function(i, j) {
				ids.push(j.id);
				var temp = _zcms_recommend.tmpl(j);
				html += temp;
			});
			$("#"+_zcms_classid).html(html);
			$("#"+_zcms_classid).trigger('onRecommendLoaded',ids.join(','));
		}
	});
};

_zcms_recommend.mq = function(map){
	var sb = [];
	for(var prop in map){
		if(map[prop]){
			sb.push(prop+"="+map[prop]);
		}
	}	
	return sb.join("&");
}

_zcms_recommend.trim = function(str){
	return str.replace(/(^\s*)|(\s*$)/g,"");
}


_zcms_recommend.cookieGet=function(name){
	var cs = document.cookie.split("; ");
	for (i = 0; i < cs.length; i++) {
		var arr = cs[i].split("=");
		var n = _zcms_recommend.trim(arr[0]);
		var v = arr[1] ? _zcms_recommend.trim(arr[1]) : "";
		if (n === name) {
			return decodeURIComponent(v);
		}
	}
	return null;
}

_zcms_recommend.format = function(fmt) { 
   var o = { 
       "M+" : this.getMonth()+1,                 //月份 
       "d+" : this.getDate(),                    //日 
       "h+" : this.getHours(),                   //小时 
       "m+" : this.getMinutes(),                 //分 
       "s+" : this.getSeconds(),                 //秒 
       "q+" : Math.floor((this.getMonth()+3)/3), //季度 
       "S"  : this.getMilliseconds()             //毫秒 
   }; 
   if(/(y+)/.test(fmt)) {
           fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
   }
   for(var k in o) {
       if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
   return fmt; 
}  

_zcms_recommend.tmpl = function(result){
	var reg=/#\{[\w_]+(,[\w_]+\([^()]*\))*}/ig;
	var result = _zcms_template.replace(reg, function(word) {
		var methods={
			format:function(value,formatstr){
				return new Date(value).format(formatstr);
			},
			charWidth:function(value,length){
				return value.substring(0,length)+"...";
			}
		};
		word=word.substring(2,word.length-1);
		var value;
		if(word.indexOf(',')>0){
        	var list=word.split(",");
        	var key=list[0];
        	value=result[key];
        	
        	var m = list[1];
        	var method = methods[m.substring(0,m.indexOf("("))];
        	var param = m.substring(m.indexOf("(")+1,m.indexOf(")"));
        	if(method){
    			value=method(value,param);
    		}
        }else{
        	value=result[word];
        }
		return value;
	});
	return result;
}

_zcms_recommend.getNext = function(){
	_zcms_pageindex++;
	var memberID=_zcms_recommend.cookieGet("_ZMemberID");
	$.ajax({
        type: 'GET',
        url: _zcms_recommend_d,
        data: {
		  memberID: memberID,
		  siteID: _zcms_siteid,
		  isPreview: _zcms_ispreview,
          type: _zcms_type,
          pageSize: _zcms_pagesize,
		  pageIndex: _zcms_pageindex,
		  fillType: _zcms_filltype
        },
        dataType: 'json',
		crossDomain : true==!(document.all),
		xhrFields: {
			withCredentials: true
		},
        success: function (result) {
			var html = '';
			var ids = [];
			$.each(result.data.data, function(i, j) {
				ids.push(j.id);
				var temp = _zcms_recommend.tmpl(j);
				html += temp;
			});
			$("#"+_zcms_classid).append(html);
			$("#"+_zcms_classid).trigger('onRecommendLoaded',ids.join(','));
		}
	});
}
